
libname hcup1999 '/disk/data3/hcup/sid/1999';
libname sidwork '/disk/data3/sid.work/millers/1999';

data states;
input state $2. fips 2.;
cards;
AZ4
HI15
WI55
OR41
NY36
IA19
run;


**Import list of chronic illness diagnosis codes;
**Downloaded from https://www.hcup-us.ahrq.gov/toolssoftware/chronic/chronic.jsp on 8/6/2014;

PROC IMPORT OUT= WORK.CCI 
            DATAFILE= "cci2012.csv" 
            DBMS=CSV REPLACE;
     GETNAMES=YES;
     DATAROW=2; 
RUN;

data cci;
set cci;
DX1=ICD_9_CM_CODE;
run;

proc sort data=cci;
by DX1;
run;

%macro sid1999;

%do i=1 %to 6;

data _null_;
set states;
if _n_=&i then do;
call symput('state', state);
call symput('fips', fips);
end;

%put &state;
%put &fips;

data sidwork.&state._sid_1999_core;
set hcup1999.&state._sid_1999_core;
FIPS=&fips;
run;

%if &i=1 %then %do;

data sidwork.sid1999;
set sidwork.&state._sid_1999_core;
run;

%end;
%else %do;

data sidwork.sid1999;
set sidwork.sid1999 sidwork.&state._sid_1999_core;
run;

%end;

%end;

data sidwork.sid1999;
set sidwork.sid1999;
count=1;
bdate=trim(left(bmonth))||trim(left(byear));
SEX=FEMALE-1;

**define diagnosis categories, from https://en.wikipedia.org/wiki/List_of_ICD-9_codes accessed 7/15/2015;

ThreeDigDx2=substr(trim(left(dx1)),1,1);

if ThreeDigDx2="E" then ecode=1;
else ecode=0;
if ThreeDigDx2="V" then ThreeDigDx=substr(DX2,1,3)*1;

else ThreeDigDx=substr(DX1,1,3)*1;
if ThreeDigDx LE 679 and ThreeDigDx GE 630 then preg=1;
else preg=0;

if threeDigDx LE 319 and ThreeDigDx GE 290 then mental=1;
else mental=0;
if threeDigDX GE 800 and threeDigDX LT 830 then fracture=1;
else fracture=0;
if threeDigDX GE 800 and threeDigDX LT 960 then injury=1;
else injury=0;
if threeDigDX=. then fracture=.;
if threeDigDX=. then mental=.;
if threeDigDX=. then injury=.;
if ThreeDigDx=540 then Appendicitis=1;
else Appendicitis=0;
if threedigdx GT 0 and threedigdx LE 139 then infect=1;
else infect=0;
if threedigdx GT 139 and threedigdx LE 239 then neoplasms=1;
else neoplasms=0;
if threedigdx GT 239 and threedigdx LE 279 then endometa=1;
else endometa=0;
if threedigdx GT 279 and threedigdx LE 289 then blood=1;
else blood=0;
if threedigdx GT 289 and threedigdx LE 319 then mentaldisorders=1;
else mentaldisorders=0;
if threedigdx GT 319 and threedigdx LE 359 then nervoussys=1;
else nervoussys=0;
if threedigdx GT 359 and threedigdx LE 389 then sense=1;
else sense=0;
if threedigdx GT 389 and threedigdx LE 459 then circulatory=1;
else circulatory=0;
if threedigdx GT 459 and threedigdx LE 519 then respiratory=1;
else respiratory=0;
if threedigdx GT 519 and threedigdx LE 579 then digestive=1;
else digestive=0;
if threedigdx GT 579 and threedigdx LE 629 then genitourinary=1;
else genitourinary=0;
if threedigdx GT 679 and threedigdx LE 709 then skin=1;
else skin=0;
if threedigdx GT 709 and threedigdx LE 739 then muscle=1;
else muscle=0;
if threedigdx GT 739 and threedigdx LE 759 then congenital=1;
else congenital=0;
if threedigdx GT 759 and threedigdx LE 779 then perinatal=1;
else perinatal=0;
if threedigdx GT 779 and threedigdx LE 799 then illdefined=1;
else illdefined=0;
if threedigdx GT 799 and threedigdx LE 999 then injpois=1;
else injpois=0;

*exclude hospitalizations related to pregnancy and delivery;

if preg=1 then delete;
run;

proc sort data=sidwork.sid1999;
by DX1;
quit;

data sidwork.sid1999;
merge sidwork.sid1999 cci;
by DX1;
if count=. then delete;
Chronic=CATEGORY_DESCRIPTION*1;
run;

proc sort data=sidwork.sid1999;
by fips;
quit;

proc means data=sidwork.sid1999 N NMISS MIN MAX MEAN SUM;
by fips;
quit;
%include 'addup1999_restat.sas';

%mend sid1999;
%sid1999
